<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>new HashMap( ); | Sean&#39;s Blog | why so serious ?</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="集合,数据结构,HashMap">
    <meta name="description" content="数据结构：数组 + 链表 &#x2F; 数组 + 红黑树  在实际编码中，我们会看到 1Map&lt;Object, Object&gt; hashMap &#x3D; new HashMap&lt;&gt;();  当然也会看到 1HashMap&lt;Object, Object&gt; hashMap &#x3D; new HashMap&lt;&gt;();  说实话，我还没有找到或是总结出一个能让我自己信服的 通俗易">
<meta property="og:type" content="article">
<meta property="og:title" content="new HashMap( );">
<meta property="og:url" content="http://www.some-note.cn/2020/10/26/new-HashMap/index.html">
<meta property="og:site_name" content="Sean&#39;s Blog">
<meta property="og:description" content="数据结构：数组 + 链表 &#x2F; 数组 + 红黑树  在实际编码中，我们会看到 1Map&lt;Object, Object&gt; hashMap &#x3D; new HashMap&lt;&gt;();  当然也会看到 1HashMap&lt;Object, Object&gt; hashMap &#x3D; new HashMap&lt;&gt;();  说实话，我还没有找到或是总结出一个能让我自己信服的 通俗易">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-10-26T07:03:08.000Z">
<meta property="article:modified_time" content="2020-10-27T09:09:23.284Z">
<meta property="article:author" content="Sean">
<meta property="article:tag" content="集合">
<meta property="article:tag" content="数据结构">
<meta property="article:tag" content="HashMap">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="Sean&#39;s Blog" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Sean</h5>
          <a href="mailto:inoubliable_s@163.com" title="inoubliable_s@163.com" class="mail">inoubliable_s@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/Sean-inoubliable" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">new HashMap( );</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">new HashMap( );</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-10-26T07:03:08.000Z" itemprop="datePublished" class="page-time">
  2020-10-26
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#介绍"><span class="post-toc-number">1.</span> <span class="post-toc-text">介绍</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#成员属性及成员方法（待补全）"><span class="post-toc-number">2.</span> <span class="post-toc-text">成员属性及成员方法（待补全）</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#成员变量"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">成员变量</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#序列化ID："><span class="post-toc-number">2.1.1.</span> <span class="post-toc-text">序列化ID：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#默认初始容量："><span class="post-toc-number">2.1.2.</span> <span class="post-toc-text">默认初始容量：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#最大容量："><span class="post-toc-number">2.1.3.</span> <span class="post-toc-text">最大容量：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#负载因子（扩容因子）："><span class="post-toc-number">2.1.4.</span> <span class="post-toc-text">负载因子（扩容因子）：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#桶的树化阈值，链表转红黑树："><span class="post-toc-number">2.1.5.</span> <span class="post-toc-text">桶的树化阈值，链表转红黑树：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#桶的树退化阈值："><span class="post-toc-number">2.1.6.</span> <span class="post-toc-text">桶的树退化阈值：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#最小树形化容量阈值："><span class="post-toc-number">2.1.7.</span> <span class="post-toc-text">最小树形化容量阈值：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#构造器"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">构造器</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Constructor-1"><span class="post-toc-number">2.2.1.</span> <span class="post-toc-text">Constructor-1</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Constructor-2"><span class="post-toc-number">2.2.2.</span> <span class="post-toc-text">Constructor-2</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Constructor-3"><span class="post-toc-number">2.2.3.</span> <span class="post-toc-text">Constructor-3</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Constructor-4"><span class="post-toc-number">2.2.4.</span> <span class="post-toc-text">Constructor-4</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#操作方式（待补全）"><span class="post-toc-number">3.</span> <span class="post-toc-text">操作方式（待补全）</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#put"><span class="post-toc-number">3.0.1.</span> <span class="post-toc-text">put()</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#get"><span class="post-toc-number">3.0.2.</span> <span class="post-toc-text">get()</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#调用频率较多的内部方法（待补全）"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">调用频率较多的内部方法（待补全）</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-new-HashMap"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">new HashMap( );</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-10-26 15:03:08" datetime="2020-10-26T07:03:08.000Z"  itemprop="datePublished">2020-10-26</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></li></ul>



            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>数据结构：数组 + 链表 / 数组 + 红黑树</p>
<hr>
<p>在实际编码中，我们会看到</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;Object, Object&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br></pre></td></tr></table></figure>

<p>当然也会看到</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">HashMap&lt;Object, Object&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br></pre></td></tr></table></figure>

<p>说实话，我还没有找到或是总结出一个能让我自己信服的 <strong>通俗易懂</strong> 的例子（如何表达）。后续有机会的话，会补充上。</p>
<hr>
<blockquote>
<p>接下来会有一些</p>
<ul>
<li>对于 HashMap 源码的展示</li>
<li>对于 HashMap 集合的操作方式</li>
</ul>
<p>建议打开 HashMap 源码，再进行阅读 直观感受会有显著提升。</p>
</blockquote>
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>首先我们会看到 HashMap 类的类头</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HashMap</span>&lt;<span class="title">K</span>,<span class="title">V</span>&gt; <span class="keyword">extends</span> <span class="title">AbstractMap</span>&lt;<span class="title">K</span>,<span class="title">V</span>&gt; <span class="keyword">implements</span> <span class="title">Map</span>&lt;<span class="title">K</span>,<span class="title">V</span>&gt;, <span class="title">Cloneable</span>, <span class="title">Serializeble</span> </span>&#123;</span><br><span class="line">    ... ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>继承 AbstractMap</p>
<blockquote>
<p>此行为不做过多解释</p>
<p><a href="http://stackoverflow.com/questions/2165204/why-does-linkedhashsete-extend-hashsete-and-implement-sete" target="_blank" rel="noopener">http://stackoverflow.com/questions/2165204/why-does-linkedhashsete-extend-hashsete-and-implement-sete</a></p>
</blockquote>
</li>
<li><p>实现 Map</p>
<blockquote>
<p>实现 Map接口，接口方法见源码，不再赘述。</p>
<ol>
<li>键不可以重复，值可以重复。</li>
<li>底层使用哈希表实现</li>
<li>线程不安全</li>
<li>允许key为null，但只允许有一条记录为null，value也可以为null，允许多条记录为null</li>
</ol>
</blockquote>
</li>
<li><p>实现 Cloneable</p>
<blockquote>
<p>点开Cloneable接口，我们可以清楚的看到什么都没有：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Cloneable</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Cloneable其实就是一个标记接口，只有实现了这个接口，然后在类中重写Object中的clone方法，然后通过类调用clone方法才能克隆成功，如果不实现这个接口，则会抛出 CloneNotSupportedException（克隆不被支持） 异常。</p>
<p>Object中的clone方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">native</span> Object <span class="title">clone</span><span class="params">()</span> <span class="keyword">throws</span> CloneNotSupportedException</span>;</span><br></pre></td></tr></table></figure>



<p>这里存在一个疑问：Object 中的 clone 方法是一个空的方法，并且有一个 native 关键字的修饰</p>
<blockquote>
<p>native 修饰的方法都是空的方法，但是这些方法都是有是实现体的，只不过 native 方法调用的实现体都是非 java 代码编写的（例如：调用的是JVM中编写的C的接口），每个 native 方法在 JVM 中都有一个同名的实现体，native 方法在逻辑上的判断都是由实现体实现的，另外这种 native 修饰的方法对返回类型、异常控制等都没有约束。</p>
<p>由此可见，判断是否实现 Cloneable 接口，是在调用 JVM 中的实现体时进行判断的。</p>
</blockquote>
<p>这里延伸出一个概念：<strong>深度克隆和浅度克隆</strong>。TODO：属于另一话题，后续找机会进行文章补充。</p>
</blockquote>
</li>
<li><p>实现 Serializeble</p>
<blockquote>
<p>Serializeble 是 Java 提供的序列化接口。</p>
<p>关于 <strong>序列化</strong> 相关内容，博客中已有相关介绍。可移步 <a href="http://localhost:4000/2020/08/27/Java序列化与反序列化/" target="_blank" rel="noopener">此处</a> 进行浏览 </p>
</blockquote>
</li>
</ul>
<h2 id="成员属性及成员方法（待补全）"><a href="#成员属性及成员方法（待补全）" class="headerlink" title="成员属性及成员方法（待补全）"></a>成员属性及成员方法（待补全）</h2><h3 id="成员变量"><a href="#成员变量" class="headerlink" title="成员变量"></a>成员变量</h3><h4 id="序列化ID："><a href="#序列化ID：" class="headerlink" title="序列化ID："></a>序列化ID：</h4><p>简单来说，java 的序列化机制是通过在运行时判断类的 serialVersionUID 来验证版本一致性的。在进行反序列时，JVM 会把传过来的字节流中的 serialVersionUID 与本地实体类中的 serialVersionUID 进行比较，如果相同则认为是一致的，便可以进行反序列化，反之回报序列化版本不一致的异常</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">362498820763181265L</span>;</span><br></pre></td></tr></table></figure>



<h4 id="默认初始容量："><a href="#默认初始容量：" class="headerlink" title="默认初始容量："></a>默认初始容量：</h4><p>二进制位移算法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> DEFAULT_INITIAL_CAPACITY = <span class="number">1</span> &lt;&lt; <span class="number">4</span>; <span class="comment">// 16</span></span><br></pre></td></tr></table></figure>



<h4 id="最大容量："><a href="#最大容量：" class="headerlink" title="最大容量："></a>最大容量：</h4><p>二进制位移算法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAXIMUM_CAPACITY = <span class="number">1</span> &lt;&lt; <span class="number">30</span>; <span class="comment">// 1073741824</span></span><br></pre></td></tr></table></figure>



<h4 id="负载因子（扩容因子）："><a href="#负载因子（扩容因子）：" class="headerlink" title="负载因子（扩容因子）："></a>负载因子（扩容因子）：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">float</span> DEFAULT_LOAD_FACTOR = <span class="number">0.75f</span>;</span><br></pre></td></tr></table></figure>



<h4 id="桶的树化阈值，链表转红黑树："><a href="#桶的树化阈值，链表转红黑树：" class="headerlink" title="桶的树化阈值，链表转红黑树："></a>桶的树化阈值，链表转红黑树：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TREEIFY_THRESHOLD = <span class="number">8</span>;</span><br></pre></td></tr></table></figure>



<h4 id="桶的树退化阈值："><a href="#桶的树退化阈值：" class="headerlink" title="桶的树退化阈值："></a>桶的树退化阈值：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> UNTREEIFY_THRESHOLD = <span class="number">6</span>;</span><br></pre></td></tr></table></figure>



<h4 id="最小树形化容量阈值："><a href="#最小树形化容量阈值：" class="headerlink" title="最小树形化容量阈值："></a>最小树形化容量阈值：</h4><p>哈希表中的容量 &gt; 该值时，才允许树形化链表（即 链表 转 红黑树）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MIN_TREEIFY_CAPACITY = <span class="number">64</span>;</span><br></pre></td></tr></table></figure>



<h3 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h3><h4 id="Constructor-1"><a href="#Constructor-1" class="headerlink" title="Constructor-1"></a>Constructor-1</h4><p>初始容量 + 负载因子</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Constructs an empty HashMap with the specified initial.</span></span><br><span class="line"><span class="comment"> * capacity and load factor.</span></span><br><span class="line"><span class="comment"> * 用指定的初始值构造一个空的HashMap</span></span><br><span class="line"><span class="comment"> * 容量和负载因子</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HashMap</span><span class="params">(<span class="keyword">int</span> initialCapacity, <span class="keyword">float</span> loadFactor)</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 判断入参初始容量是否 &lt; 0</span></span><br><span class="line">    <span class="keyword">if</span> (initialCapacity &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(... ...);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断入参初始容量是否 &gt; HashMap集合的最大容量</span></span><br><span class="line">    <span class="keyword">if</span> (initialCapacity &gt; MAXIMUM_CAPACITY) &#123;</span><br><span class="line">        initialCapacity = MAXUMUM_CAPACITY;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断入参的负载因子是否 &lt;= 0</span></span><br><span class="line">    <span class="comment">// NaN: NaN是一个Float对象；NaN对象的内存地址尽不相同。此处类同对比内存地址的 equals() 方法</span></span><br><span class="line">    <span class="keyword">if</span> (loadFactor &lt;= <span class="number">0</span> || Float.isNaN(loadFactor)) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(... ...);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 负载因子</span></span><br><span class="line">    <span class="keyword">this</span>.loadFactor = loadFactor;</span><br><span class="line">    <span class="comment">// 1.集合容量。2.tableSizeFor()：运用二进制位或，返回大于 ‘输入初始容量’ 且 ‘最近的2的整数次幂’的数</span></span><br><span class="line">    <span class="keyword">this</span>.threshold = tableSizeFor(initialCapacity);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="Constructor-2"><a href="#Constructor-2" class="headerlink" title="Constructor-2"></a>Constructor-2</h4><p>初始容量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Constructs an empty HashMap with the specified initial.</span></span><br><span class="line"><span class="comment"> * capacity and the default load factor(0.75).</span></span><br><span class="line"><span class="comment"> * 用指定的初始值构造一个空的HashMap</span></span><br><span class="line"><span class="comment"> * 容量和默认的负载因子</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">hashMap</span><span class="params">(<span class="keyword">int</span> initalCapacity)</span> </span>&#123; </span><br><span class="line">    <span class="comment">// 入参初始容量，0.75f负载因子</span></span><br><span class="line">    <span class="keyword">this</span>(initialCapacity, DEFAULT_LOAD_FACTOR);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="Constructor-3"><a href="#Constructor-3" class="headerlink" title="Constructor-3"></a>Constructor-3</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Constructs an empty HashMap with the default initial.</span></span><br><span class="line"><span class="comment"> * default capacity(16) and the default load factor(0.75).</span></span><br><span class="line"><span class="comment"> * 用指定的初始值构造一个空的HashMap</span></span><br><span class="line"><span class="comment"> * 容量和默认的负载因子</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HashMap</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 0.75负载因子</span></span><br><span class="line">    <span class="keyword">this</span>.loadFactor = DEFAULT_LOAD_FACTOR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="Constructor-4"><a href="#Constructor-4" class="headerlink" title="Constructor-4"></a>Constructor-4</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Constructs a new HashMap with the same mappings as the specified Map.</span></span><br><span class="line"><span class="comment"> * The HashMap is created with default load factor (0.75) and an initial capacity          	* sufficient to hold the mappings in the specified Map.</span></span><br><span class="line"><span class="comment"> * 构造一个新的HashMap，其KV映射与指定的映射相同。</span></span><br><span class="line"><span class="comment"> * 这个新的HashMap使用默认的负载因子(0.75) 和 足以容纳其大小的初始容量进行过创建。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HashMap</span><span class="params">(Map&lt;? extends K, ? extends V&gt; m)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 默认负载因子(0.75)</span></span><br><span class="line">    <span class="keyword">this</span>.loadFactor = DEFAULT_LOAD_FACTOR;</span><br><span class="line">    <span class="comment">// 此方法实现了Map接口的pullAll 和 Map的构造器</span></span><br><span class="line">    putMapEntries(m, <span class="keyword">false</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// <span class="doctag">TODO:</span> 待解析</span></span><br><span class="line"><span class="function"><span class="keyword">final</span> <span class="keyword">void</span> <span class="title">putMapEntries</span><span class="params">(Map&lt;? extends K, ? extends V&gt; m, <span class="keyword">boolean</span> evict)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 入参Map集合的大小</span></span><br><span class="line">    <span class="keyword">int</span> s = m.size();</span><br><span class="line">    <span class="keyword">if</span> (s &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (table == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">float</span> ft = ((<span class="keyword">float</span>)s / loadFactor) + <span class="number">1.0F</span>;</span><br><span class="line">            <span class="keyword">int</span> t = ((ft &lt; (<span class="keyword">float</span>)MAXIMUM_CAPACITY)) ? (<span class="keyword">int</span>) ft : MAXIMUM_CAPACITY;</span><br><span class="line">            <span class="keyword">if</span> (t &gt; threshold) &#123;</span><br><span class="line">                threshold = tableSizeFor(t);</span><br><span class="line">            &#125;</span><br><span class="line">          <span class="comment">// 被复制集合的容量是否大于新集合的容量</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (s &gt; threashold) &#123;</span><br><span class="line">            resize();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 将入参Map的K-V循环导入新集合中</span></span><br><span class="line">        <span class="keyword">for</span> (Map.Entry&lt;? extends K, ? extends V&gt; e : m.entrySet()) &#123;</span><br><span class="line">            K key = e.getKey();</span><br><span class="line">            V value = e.getValue();</span><br><span class="line">            <span class="comment">// 注意：putVal()方法、hash()方法、onlyIfAbsent、evict</span></span><br><span class="line">            putVal(hash(key), key, value, <span class="keyword">false</span>, evict);</span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="操作方式（待补全）"><a href="#操作方式（待补全）" class="headerlink" title="操作方式（待补全）"></a>操作方式（待补全）</h2><h4 id="put"><a href="#put" class="headerlink" title="put()"></a>put()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String, Object&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">hashMap.put(<span class="string">"1"</span>, <span class="string">"Joker"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// HashMap类中的put</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> V <span class="title">put</span><span class="params">(K key, V value)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// key的hash值</span></span><br><span class="line">    <span class="keyword">return</span> putVal(hash(key), key, value, <span class="keyword">false</span>, <span class="keyword">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="get"><a href="#get" class="headerlink" title="get()"></a>get()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String, Object&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">hashMap.get(<span class="string">"1"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// HashMap类中的get</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> V <span class="title">get</span><span class="params">(Object key)</span> </span>&#123;</span><br><span class="line">    Node&lt;K, V&gt; e;</span><br><span class="line">    <span class="comment">// getNode()</span></span><br><span class="line">    <span class="keyword">return</span> (e = getNode(hash(key), key)) == <span class="keyword">null</span> ? <span class="keyword">null</span> : e.value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h3 id="调用频率较多的内部方法（待补全）"><a href="#调用频率较多的内部方法（待补全）" class="headerlink" title="调用频率较多的内部方法（待补全）"></a>调用频率较多的内部方法（待补全）</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// hash() 方法</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> <span class="title">hash</span><span class="params">(Object key)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> h;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在源码中我们可以看到 public native int hashCode(); 方法</span></span><br><span class="line"><span class="comment">     * 其由C语言实现或JVM中实现。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">return</span> (key == <span class="keyword">null</span>) ? <span class="number">0</span> : (h = key.hashCode()) ^ (h &gt;&gt;&gt; <span class="number">16</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// putVal() 方法</span></span><br><span class="line"><span class="function"><span class="keyword">final</span> V <span class="title">putVal</span><span class="params">(<span class="keyword">int</span> hash, K key, V value, <span class="keyword">boolean</span> onlyIfAbsent, <span class="keyword">boolean</span> evict)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 后续待补充其比较方式，触发扩容等 TODO：... ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-10-27T09:09:23.284Z" itemprop="dateUpdated">2020-10-27 17:09:23</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/2020/10/26/new-HashMap/" target="_blank" rel="external">http://www.some-note.cn/2020/10/26/new-HashMap/</a>
        
    </div>
    
    <footer>
        <a href="http://www.some-note.cn">
            <img src="/img/avatar.jpg" alt="Sean">
            Sean
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/HashMap/" rel="tag">HashMap</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag">数据结构</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%9B%86%E5%90%88/" rel="tag">集合</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://www.some-note.cn/2020/10/26/new-HashMap/&title=《new HashMap( );》 — Sean's Blog&pic=http://www.some-note.cn/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://www.some-note.cn/2020/10/26/new-HashMap/&title=《new HashMap( );》 — Sean's Blog&source=何必那么认真" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://www.some-note.cn/2020/10/26/new-HashMap/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《new HashMap( );》 — Sean's Blog&url=http://www.some-note.cn/2020/10/26/new-HashMap/&via=http://www.some-note.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://www.some-note.cn/2020/10/26/new-HashMap/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between flex-row-reverse">
  

  
    <div class="waves-block waves-effect next">
      <a href="/2020/10/26/%E6%B5%85%E8%B0%88%E5%AF%B9%E4%BA%8EJava%E4%B8%AD-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%90%86%E8%A7%A3/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">浅谈对于Java中-面向对象的理解</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        喝杯咖啡
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        

        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Sean &copy; 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://www.some-note.cn/2020/10/26/new-HashMap/&title=《new HashMap( );》 — Sean's Blog&pic=http://www.some-note.cn/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://www.some-note.cn/2020/10/26/new-HashMap/&title=《new HashMap( );》 — Sean's Blog&source=何必那么认真" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://www.some-note.cn/2020/10/26/new-HashMap/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《new HashMap( );》 — Sean's Blog&url=http://www.some-note.cn/2020/10/26/new-HashMap/&via=http://www.some-note.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://www.some-note.cn/2020/10/26/new-HashMap/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://www.some-note.cn/2020/10/26/new-HashMap/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>








<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
